# 导包
import redis

# 定义ip地址和端口
host = '127.0.0.1'
port = 6379

# # 定义连接对象
r = redis.Redis(host=host, port=port)

# 1.list的使用 lpush：从头部添加  rpush：从尾部添加
# r.lpush('testlist', 1)
# print(r.llen('testlist'))


# # 2.存键值对
# r.set('test', 'test')

# # 取出键值对
# mytest = r.get('test')

# # 转码
# mytest = mytest.decode('utf-8')

# print(mytest)


# 3.有序集合
r.zadd('myrank', {'car': 100})
r.zadd('myrank', {'phone': 200})
# r.delete('myrank')

# 修改操作
newscore = r.zincrby('myrank', 300, 'phone')
# print(newscore)

# 遍历有序集合
# print(r.zrange('myrank', 0, -1, desc=True, withscores=True))

# 赋值
myrank = r.zrange('myrank', 0, -1, desc=True, withscores=True)

# 设置过期时间
# r.expire('myrank', 60)

# 显示倒数时间
print(r.ttl('myrank'))

# 取值(转码)
print(str(myrank[0][0], 'utf-8'))
